package
{
	import flash.geom.Point;
	
	import org.flixel.*;
	/**
	 * The Monster class could be extended or it could just have a lot of different images
	 * and if statements for setting information like Monster Speed. As there is only one
	 * monster at the moment this is undecided.
	**/
	public class Monster extends FlxSprite
	{
		protected var theDirection:int;
		protected var randomChange:int;
		protected var speed:int;
		
		public function Monster(start:Point, type:int)
		{
			super(start.x, start.y, null);
			addAnimation("run", [0,1,2,3],6);
			//addAnimation("thrown", [4,5,6,7,8,9,10,11], 12);
			play("run");
			speed = 60;
			theDirection = 0;
			changeDirection();
			
			if (type == 0) {
				loadGraphic(Res.grassMonsterImage, true, false, 30, 30, false);
			} else if (type == 1) {
				loadGraphic(Res.barrenMonsterImage, true, false, 30, 30, false);
			} else {
				loadGraphic(Res.voidMonsterImage, true, false, 30, 30, false);
			}
			
			randomChange = Math.floor(Math.random() * 200 + 100);
		}
		
		
		public override function update():void
		{
			if (Elemental.FREEZE) {
				velocity.x = 0;
				velocity.y = 0;
			} else {
				if(randomChange == 0) {
					changeDirection();
					randomChange = Math.floor(Math.random() * 600 + 300);
				} else {
					randomChange--;
				}
			}
		}
		
		public function changeDirection():void {
			theDirection = Math.floor(Math.random() * 4);
			if (theDirection == 0) {
				velocity.y = speed;
			} else if (theDirection == 1) {
				velocity.y = -speed;
			} else if (theDirection == 2) {
				velocity.x = speed;
			} else {
				velocity.x = -speed;
			}
		}
		
		public function toPlayer(PlayerPoint:Point):void {
			var temp1:Point;
			var angle:Number;
			temp1 = new Point(this.x, this.y);
			angle = getAngle(temp1, PlayerPoint);
			this.velocity.x = speed * Math.cos(angle);
			this.velocity.y = speed * Math.sin(angle);
		}
		
		/**
		 * basic function for getting an angle between two points.
		 **/
		protected function getAngle(start:Point, end:Point):Number {
			var angle:Number = Math.atan2((end.y - start.y) , (end.x - start.x));
			return angle;
		}
	}
}